/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cz.dusa.worklog.datalib;

import java.io.Serializable;
import java.sql.Date;
import java.sql.Time;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**
 * Class representing one work day. Each work day is related to a project. The work
 * at the day starts with StartOfWork and ends with EndOfWork. There can be a interruption
 * of the work within this time. In such case the attribute minusTime should contain amount
 * of hours, that has not been used for work.
 * @author vdusa
 * @see Project
 */
@Entity
@Table(name = "WORKDAY", schema = "APP")
public class WorkDay implements Serializable, WorklogData {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @ManyToOne
    private Project project;
    private Date workDate;
    private String description;
    private Time startOfWork;
    private Time endOfWork;
    private Double minusTime;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof WorkDay)) {
            return false;
        }
        WorkDay other = (WorkDay) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "cz.dusa.worklog.datalib.WorkDay[id=" + id + "]";
    }

    /**
     * @return the project 
     */
    public Project getProject() {
        return project;
    }

    /**
     * @param project the project to set
     */
    public void setProject(Project project) {
        this.project = project;
    }

    /**
     * @return the workDate
     */
    public Date getWorkDate() {
        return workDate;
    }

    /**
     * @param workDate the workDate to set
     */
    public void setWorkDate(Date workDate) {
        this.workDate = workDate;
    }

    /**
     * @return the description
     */
    public String getDescription() {
        return description;
    }

    /**
     * @param description the description to set
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * @return the startOfWork
     */
    public Time getStartOfWork() {
        return startOfWork;
    }

    /**
     * @param startOfWork the startOfWork to set
     */
    public void setStartOfWork(Time startOfWork) {
        this.startOfWork = startOfWork;
    }

    /**
     * @return the endOfWork
     */
    public Time getEndOfWork() {
        return endOfWork;
    }

    /**
     * @param endOfWork the endOfWork to set
     */
    public void setEndOfWork(Time endOfWork) {
        this.endOfWork = endOfWork;
    }

    /**
     * @return the minusTime (e.g. time used for lunch)
     */
    public Double getMinusTime() {
        return minusTime;
    }

    /**
     * @param minusTime the minusTime to set
     */
    public void setMinusTime(Double minusTime) {
        this.minusTime = minusTime;
    }

    /**
     * Returns total work time at the day.
     * That is: amount of hours between end of work and start of work minus the minusTime().
     * @see getMinusTime()
     */
    public Double getTotalWork(){
        double hours = 0;

        if(getStartOfWork() != null && getEndOfWork() != null){
            long millis = getEndOfWork().getTime() - getStartOfWork().getTime();
            hours = ((Long)millis) / 1000.0 / 60.0 / 60.0;
            if (getMinusTime() != null){
                hours -= getMinusTime();
            }
        }
        
        return hours;
    }

}
